package com.doryar.girl.controller;

import com.doryar.girl.domain.Girl;
import com.doryar.girl.repository.GirlRepository;
import com.doryar.girl.service.GirlService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
public class GirlController {
    @Autowired
    private GirlRepository girlRepository;
    @Autowired
    private GirlService girlService;
    /**
     * @return     * 查询所以女生的列表
     */

    @GetMapping(value="/girls")
    public List<Girl> girlList(){
        return girlRepository.findAll();
    }

    //增加一个女生
    @PostMapping(value="/girls")
    public Girl girlAdd(@Valid Girl girl,   BindingResult bindingResult) {
        if(bindingResult.hasErrors())
        {
            System.out.print(bindingResult.getFieldError().getDefaultMessage());
            return null;
        }
    girl.setCupSize(girl.getCupSize());
    girl.setAge(girl.getAge());
    return  girlRepository.save(girl);
    }
    //查询
    @GetMapping(value="/girls/{id}")
    public Girl girlFindOne(@PathVariable("id") Integer id){
        return girlRepository.findById(id).get();
    }
    //更新
    @PutMapping(value="/girls/{id}")
    public Girl girlUpdate(@PathVariable("id") Integer id,
                           @RequestParam("age") Integer age,
                           @RequestParam("cupSize") String cupSize){
        Girl girl=new Girl();
        girl.setId(id);
        girl.setAge(age);
        girl.setCupSize(cupSize);
        return girlRepository.save(girl);
    }
     @DeleteMapping(value="/girls/{id}")
     public void girlDelete(@PathVariable("id") Integer id){
         girlRepository.deleteById(id);
     }
    //通过年龄查询
    @GetMapping(value="/girls/age/{id}")
    public List<Girl> girlListByAge(@PathVariable("id") Integer id){
        return girlRepository.findByAge(id);
    }

    @PostMapping(value="/girls/two")
    public void girlTwo(){
        girlService.insertTwo();
    }
}
